package com.spzx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.core.utils.poi.ExcelUtil;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.domain.UserInfoVo;
import com.spzx.user.service.UserAddressService;
import com.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 会员Controller
 *
 * @author atguigu
 * @date 2024-11-01
 */
@Tag(name = "会员接口管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 查询会员列表
     */
    @Operation(summary = "查询会员列表")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo) {
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @Operation(summary = "导出会员列表")
    @RequiresPermissions("user:userInfo:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo) {
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @Operation(summary = "获取会员详细信息")
    @RequiresPermissions("user:userInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userInfoService.getById(id));
    }

    /**
     * 新增会员
     */
    @Operation(summary = "新增会员")
    @RequiresPermissions("user:userInfo:add")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.save(userInfo));
    }

    /**
     * 修改会员
     */
    @Operation(summary = "修改会员")
    @RequiresPermissions("user:userInfo:edit")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.updateById(userInfo));
    }

    /**
     * 删除会员
     */
    @Operation(summary = "删除会员")
    @RequiresPermissions("user:userInfo:remove")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userInfoService.removeBatchByIds(Arrays.asList(ids)));
    }


    @Operation(summary = "根据用户id查询用户的收货地址")
    @GetMapping("/getUserAddress/{userId}")
    public AjaxResult getUserAddressByUserId(@PathVariable Long userId) {
        //调用自带的带条件查询的方法
        List<UserAddress> userAddressList = userAddressService.list(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId, userId));
        return AjaxResult.success(userAddressList);
    }

    @Operation(summary = "根据会员id和状态值禁用或启用会员")
    @GetMapping("/{userId}/{status}")
    public AjaxResult updateStatusByUserIdAndStatus(@PathVariable Long userId, @PathVariable Integer status) {
        //调用自带的带条件更新的方法
        userInfoService.update(new LambdaUpdateWrapper<UserInfo>().set(UserInfo::getStatus, status).eq(UserInfo::getId, userId));
        return AjaxResult.success();
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/getLoginUserInfo")
    public AjaxResult getLoginUserInfo(HttpServletRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return success(userInfoVo);
    }

    @Operation(summary = "判断用户是否收藏了某商品")
    @GetMapping("/isCollect/{skuId}")
    public AjaxResult isCollect(@PathVariable Long skuId){
        //调用UserInfoService中判断用户是否收藏的方法
        Boolean flag = userInfoService.isCollect(skuId);
        return AjaxResult.success(flag);
    }

    @Operation(summary = "收藏商品")
    @GetMapping("/collect/{skuId}")
    public AjaxResult collect(@PathVariable Long skuId){
        //调用UserInfoService中收藏商品的方法
        Boolean flag = userInfoService.collect(skuId);
        return AjaxResult.success(flag);
    }

    @Operation(summary = "取消收藏商品")
    @GetMapping("/cancelCollect/{skuId}")
    public AjaxResult cancelCollect(@PathVariable Long skuId){
        //调用UserInfoService中收藏商品的方法
        Boolean flag = userInfoService.cancelCollect(skuId);
        return AjaxResult.success(flag);
    }

    @Operation(summary = "查询用户浏览记录")
    @GetMapping("/userBrowseHistoryList/{pageNum}/{pageSize}")
    public TableDataInfo getUserBrowseHistoryList(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //调用UserInfoService中查询用户浏览器记录的方法
        List<ProductSku> productSkuList = userInfoService.getUserBrowseHistoryList();
        return getDataTable(productSkuList);
    }


    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserInfo userInfo) {
        userInfoService.register(userInfo);
        return R.ok();
    }

    @Operation(summary = "根据用户名获取用户信息")
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<UserInfo> getUserInfo(@PathVariable("username") String username) {
        UserInfo userInfo = userInfoService.selectUserByUserName(username);
        return R.ok(userInfo);
    }

    @Operation(summary = "更新用户登录信息")
    @InnerAuth
    @PutMapping("/updateUserLogin")
    public R<Boolean> updateUserLogin(@RequestBody UpdateUserLogin updateUserLogin) {
        return R.ok(userInfoService.updateUserLogin(updateUserLogin));
    }
}